package com.sawyerqiu.constant;

import com.sawyerqiu.model.OrderExcelDataBuilder;
import org.apache.commons.math3.util.Pair;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : SawyerQiu
 * @date : 2024/7/22 17:41
 */
public enum OrderExcelDataParseType {

    Normal {
        @Override
        public Pair<Integer, List<OrderExcelDataBuilder>> parse(Date date, List<String> strings, int temp) throws Exception {
            List<OrderExcelDataBuilder> list = new ArrayList<>();

            String adress = strings.get(temp);
            temp += 1;
            if(adress.contains(":")){
                throw new Exception("数据格式错误，地址：" + adress);
            }

            String name = strings.get(temp).split(":")[1];
            temp += 1;

            String phone = strings.get(temp).split(":")[1];
            temp += 1;

            temp += 1;

            temp = tryGetOrderExcelData(date, strings, temp, list, adress, name, phone);

            return Pair.create(temp, list);
        }
    },

    FindHistory {
        @Override
        public Pair<Integer, List<OrderExcelDataBuilder>> parse(Date date, List<String> strings, int temp) {
            List<OrderExcelDataBuilder> list = new ArrayList<>();

            String name = strings.get(temp);
            temp += 1;

            temp = tryGetOrderExcelData(date, strings, temp, list, null, name, null);

            return Pair.create(temp, list);
        }
    };

    private static int tryGetOrderExcelData(Date date, List<String> strings, int temp, List<OrderExcelDataBuilder> list, String adress, String name, String phone) {
        if (temp >= strings.size()) {
            return temp;
        }

        String goodAndWeightStr = strings.get(temp);
        temp += 1;

        if (!goodAndWeightStr.contains("斤")) {
            return temp;
        }


        String[] goodAndWeight = goodAndWeightStr.split(":");

        String goods = goodAndWeight[0];
        String weightStr = goodAndWeight[1];
        float weight = Float.parseFloat(weightStr.substring(0, weightStr.length() - 1));

        OrderExcelDataBuilder orderExcelDataBuilder = OrderExcelDataBuilder.newBuilder();
        orderExcelDataBuilder.setBuyer(name + (phone == null ? "" : String.format("（%s）", phone)));
        orderExcelDataBuilder.setDate(date);
        orderExcelDataBuilder.setGoods(goods);
        orderExcelDataBuilder.setWeight(weight);

        list.add(orderExcelDataBuilder);

        return tryGetOrderExcelData(date, strings, temp, list, adress, name, phone);
    }

    public abstract Pair<Integer, List<OrderExcelDataBuilder>> parse(Date date, List<String> strings, int temp) throws Exception;

}
